<?php
/**
 * Created by ptmall
 * User : 河南邦耀网络科技有限公司
 * Date : 2020/9/10
 * Time : 16:09
 */

namespace app\api\controller;


use app\common\controller\Api;
use fast\Http;
use think\Config;
use think\Db;

class Pay extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @Explain :支付回调
     * @throws \think\exception\DbException
     * @Date : 2020/9/23 16:42
     * @Author : 河南邦耀网络科技有限公司
     */

    public function kaiYiReceive(){
        $state=trim($_GET['state']);
        $customerid=trim($_GET['customerid']);
        $sd51no=trim($_GET['sd51no']);
        $orderId=$sdcustomno=trim($_GET['sdcustomno']);
        $ordermoney=trim($_GET['ordermoney']);
        $mark=trim($_GET['mark']);
        $sign=trim($_GET['sign']);
        $resign=trim($_GET['resign']);
        $des=trim($_GET['des']);
        $key=Config::get("data_conf.secret");
        $signstr='customerid='.$customerid.'&sd51no='.$sd51no.'&sdcustomno='.$sdcustomno.'&mark='.$mark.'&key='.$key.'';
        file_put_contents(RUNTIME_PATH.'pay_sign.txt',$signstr);
        $signtmp=strtoupper(md5($signstr,false));
        $signstr2='sign='.$signtmp.'&customerid='.$customerid.'&ordermoney='.$ordermoney.'&sd51no='.$sd51no.'&state='.$state.'&key='.$key.'';
        $signtmp2=strtoupper(md5($signstr2,false));
        if ($sign==$signtmp && $resign==$signtmp2) {
            if ($state==1) {
                if(strpos($sdcustomno,"agent") !== false) {
                    $orderInfo=\app\admin\model\agent\Order::get(['orderId'=>$sdcustomno]);
                }elseif(strpos($orderId,"pt") !== false){
                    $orderInfo=\app\admin\model\order\Order::get(['orderId'=>$sdcustomno]);
                }
                if(empty($orderInfo)){
                    $this->error('订单信息错误');
                }
                if($orderInfo['payStatus']==1){
                    $this->success('订单已支付');
                }
                $payType=$orderInfo['payType'];

                try{
                    $transaction_id=$sd51no;
                    if(strpos($sdcustomno,"agent") !== false) {
                        \app\admin\model\agent\Order::agentPay($orderId, $transaction_id, $payType);
                    }elseif(strpos($orderId,"pt") !== false){
                        \app\admin\model\SpellOrder::startPay($orderId, $transaction_id, $payType);
                    }
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
                if((strpos($orderId,"pt") !== false) && $orderInfo['model']==2)  \app\common\library\Sms::sendOrderState($orderId,$orderInfo);
                file_put_contents(RUNTIME_PATH.'pay.txt','支付成功'.$sdcustomno);
            }
            else{
//商户业务数据失败处理
            }
//处理完后返回接收到标识为1
//只要商户接到通知，不管订单状态如何
//都只需要返回接收到标识为1
            echo "<result>1</result>";
        }
        else {
            echo "customerid=".$customerid."&sd51no=".$sd51no."&sdcustomno=".$sdcustomno."&mark=".$mark."&key=".$key;
        }
    }

    public function kaiYiBack(){
        $orderId=$this->request->request('orderId','');
        $order=\app\admin\model\order\Order::get(['orderId'=>$orderId]);
        if(strpos($orderId,"agent") !== false) {
            header('Location:ptsc2020://?url=/upgradeAgent&orderId='.$orderId);
        }elseif(strpos($orderId,"pt") !== false) {
            if($order['isApp']==0){
//                header('Location: https://ptshangcheng.oss-cn-beijing.aliyuncs.com/download/android/%E5%BA%93%E5%B0%9A%E5%95%86%E5%9F%8E.apk');
                header('Location: http://ptcs-share.rztx168.com/share/phoneBills.html?orderId='.$orderId);
                return;
            }
            header('Location:ptsc2020://?url=/paySuccess&orderId='.$orderId);
        }

    }


}
